package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /*
    * 新增菜品,口味
    * */
    @Override
    @Transactional//开启事务管理,保证两个表的数据一致
    public void saveWithFlavor(DishDTO dishDTO) {
        //创建实体对象
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);

        //插入一条菜品数据
        dishMapper.insert(dish);

        //获取insert语句主键值id
        Long dishId = dish.getId();


        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            //将菜品新增过后的主键值id赋给口味dishID
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishId);
            });

            //向口味表插入多条数据
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    /*
    * 菜品分页查询
    * */
    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> page =  dishMapper.pageQuery(dishPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /*
    * 批量删除信息,参数ids是菜品dish的id
    * */
    @Transactional//开启事务管理,保证两个表的数据一致
    @Override
    public void deletebatch(List<Long> ids) {
        //判断菜品是否处于启用状态,如果是,则不能删除
        for(Long id : ids){
            //根据菜品id查询菜品信息
           Dish dish = dishMapper.getById(id);
            if (dish.getStatus() == StatusConstant.ENABLE) {
                //当前菜品处于启用状态,不能删除
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //判断菜品是否关联着套餐,如果有关联,则不能删除
        List<Long> setmealIds = setmealDishMapper.getSetmealIdByDishIds(ids);
        if(setmealIds != null && setmealIds.size() > 0){
            //说明菜品有关联套餐,不能删除
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //根据菜品id删除菜品
        for(Long id : ids){
            dishMapper.deleteById(id);
            //删除跟菜品相关联的口味表flavor
            dishFlavorMapper.deleteByDishId(id);
        }

    }

    /*
    * 根据ID查询菜品信息,进行修改回写
    * */
    @Override
    public DishVO getByIdWithFlavor(Long id) {
        //根据id查询dish表中的数据
        Dish dish = dishMapper.getById(id);
        //根据dish_id查询Flaovr表的所有口味,每个菜品口味众多,所以用一个集合来接受
       List<DishFlavor> df = dishFlavorMapper.getByDishId(id);

        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(df);
        return dishVO;
    }

    /*
    * 根据id修改菜品信息和菜品口味
    * */
    @Override
    @Transactional
    public void updateByIdWithFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        //操作菜品表对菜品进行修改
        dishMapper.updateById(dish);
        //根据菜品id将口味删除
        dishFlavorMapper.deleteByDishId(dishDTO.getId());
        //向口味表插入数据,接收到前端传过来的口味
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dish.getId());
            });
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    /*
    * 菜品的启用禁用操作
    * */
    @Override
    public void startOrStop(Integer status, long id) {
        //创建一个dish对象
        Dish dish = Dish.builder()
                .status(status)
                .id(id)
                .build();
        //根据ID修改status的状态,将一整个dish对象传过去进行动态修改(声明这里可以不创建dish对象直接把status和id传过去,但是为了后面需要修改状态的接口共用,所以这里直接创建了一个动态的sql)
        dishMapper.update(dish);

    }

    /*
    * 根据分类id查询菜品信息
    * */
    @Override
    public List<Dish> getList(Long categoryId) {
//        Dish dish = new Dish();
//        dish.setCategoryId(categoryId);
//        dish.setStatus(StatusConstant.ENABLE);
        List<Dish> dishList =  dishMapper.getList(categoryId);
        return dishList;
    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.getList(dish.getCategoryId());

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
